import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class Returns_management extends JFrame {

    final static String DRIVER="com.mysql.cj.jdbc.Driver";
    final static String DB_URL="jdbc:mysql://localhost:3306/java_sql";//-----修改mysql连接
    final static String USER="root",PASS="0000";

    //MySql查询结果列表框
    String[] TH = new String[] {"条形码", "商品名称", "数量", "交易编码"};
    String[][] TB = new String[1][4];
    JTable table = new JTable(TB,TH);
    TableModel tableModel=new DefaultTableModel(TB,TH);
    JScrollPane sp = new JScrollPane(table);
    //主窗口
    JFrame frame = new JFrame("退货管理");
    //提示框
    JDialog d2 = new JDialog(frame,"错误",false);//非模式对话框
    JDialog d1 = new JDialog(frame,"退货",false);//非模式对话框
    JDialog d3 = new JDialog(frame,"成功",false);//非模式对话框
    JDialog d4 = new JDialog(frame,"错误",false);//非模式对话框
    JDialog d5 = new JDialog(frame,"错误",false);//非模式对话框
    //其他设置
    String str1;
    String str2;
    boolean bo = false;
    //mian方法
    public static void main(String[] args) {
        new Returns_management().init();;
    }
//主界面
    //界面外观设计
    public void init() {
        
        //窗口大小、位置
        MainSetWindow(frame);

        //菜单条
        MenuBar menuBar = new MenuBar();
        //菜单
        Menu m1 = new Menu("首页");
        Menu m2 = new Menu("入库管理");
        Menu m3 = new Menu("出库管理");
        Menu m4 = new Menu("用户管理");
        Menu m5 = new Menu("商品管理");
        Menu m6 = new Menu("个人信息");
        //菜单项
        MenuItem m1_1 = new MenuItem("主页面");
        MenuItem m1_2 = new MenuItem("退出");
        MenuItem m2_1 = new MenuItem("获取条形码信息");
        MenuItem m2_2 = new MenuItem("入库信息");
        MenuItem m2_3 = new MenuItem("入库列表");
        MenuItem m3_1 = new MenuItem("获取条形码信息");
        MenuItem m3_2 = new MenuItem("出库信息");
        MenuItem m3_3 = new MenuItem("出库列表");
        MenuItem m4_1 = new MenuItem("增加用户");
        MenuItem m4_2 = new MenuItem("重置用户密码");
        MenuItem m4_3 = new MenuItem("解锁用户");
        MenuItem m4_4 = new MenuItem("增加角色");
        MenuItem m4_5 = new MenuItem("分配角色");
        MenuItem m4_6 = new MenuItem("分配权限");
        MenuItem m5_1 = new MenuItem("获取条形码信息");
        MenuItem m5_2 = new MenuItem("商品交易记录");
        MenuItem m5_3 = new MenuItem("商品交易列表");
        MenuItem m6_1 = new MenuItem("个人信息完善");
        MenuItem m6_2 = new MenuItem("密码修改");
        //菜单布局
        m1.add(m1_1);
        m1.add(m1_2);
        m2.add(m2_1);
        m2.add(m2_2);
        m2.add(m2_3);
        m3.add(m3_1);
        m3.add(m3_2);
        m3.add(m3_3);
        m4.add(m4_1);
        m4.add(m4_2);
        m4.add(m4_3);
        m4.add(m4_4);
        m4.add(m4_5);
        m4.add(m4_6);
        m5.add(m5_1);
        m5.add(m5_2);
        m5.add(m5_3);
        m6.add(m6_1);
        m6.add(m6_2);
        menuBar.add(m1);
        menuBar.add(m2);
        menuBar.add(m3);
        menuBar.add(m4);
        menuBar.add(m5);
        menuBar.add(m6);
        //文本标签
        Label l1 = new Label("退货管理",Label.CENTER);
        Label l2 = new Label("交易编码");

        //按钮
        Button b1 = new Button("查询交易记录");
        Button b2 = new Button("退货");
        
        //文本框
        TextField tf = new TextField(30);//查询框
        
        //容器
        Panel p1 = new Panel();//标签和查询文本框
        Panel p2 = new Panel();//查询和确认按钮
        //Panel p3 = new Panel();//查询显示区标题

        //布局
        Box vBox = Box.createVerticalBox();
        vBox.add(l1);//退货管理标题
            p1.setLayout(new FlowLayout(FlowLayout.CENTER));
            p1.add(l2);
            p1.add(tf);
        vBox.add(p1);//标签和查询文本框
            p2.setLayout(new FlowLayout(FlowLayout.CENTER));
            p2.add(b1);
            p2.add(b2);
        vBox.add(p2);//查询和确认按钮
        //vBox.add(p3);
        frame.setMenuBar(menuBar);
        frame.add(vBox,BorderLayout.NORTH);
        frame.add(sp);
        frame.setVisible(true);
    //各个监听器
        //查询按钮监听器
        b1.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                String st = tf.getText();
                selectSQL(st);
            }
        });
        //确认退货按钮监听器
        b2.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                retutrns();
            }
        });
    }

    //按钮1 - MySQL查询
    public void selectSQL(String st){
        Connection conn=null;
        Statement stat=null;
        try {
			conn = DriverManager.getConnection(DB_URL, USER, PASS);
			stat = conn.createStatement();
            if(st.length() == 0){
                Label l1 = new Label("请输入交易编码 !",Label.CENTER);//退货管理
                DialogSetWindow(d2);
                d2.add(l1,BorderLayout.CENTER);
                d2.setVisible(true);
            }else{
                if(st.length() != 0){
                ResultSet rs = stat.executeQuery("SELECT gd_bar_code,gd_broad_name,dt_gd_num,tr_uuid FROM sp_detailed where tr_uuid ="+st);//----输入的交易编码不能带“-”符号
                rs.next();
                TB[0][0]=rs.getString("gd_bar_code");
                TB[0][1]=rs.getString("gd_broad_name");
                TB[0][2]=rs.getString("dt_gd_num");
                TB[0][3]=rs.getString("tr_uuid");
                str1 = TB[0][0];
                str2 = TB[0][2];
                bo = true;
                }
            }
            TableModel tableModel=new DefaultTableModel(TB,TH);
            table.setModel(tableModel);
		} catch (Exception e) {
            System.out.println(e);
            Label l1 = new Label("交易编码无效 !",Label.CENTER);//退货管理
            DialogSetWindow(d4);
            d4.add(l1,BorderLayout.CENTER);
            d4.setVisible(true);
            for(int i=0;i<4;i++){
                TB[0][i] = " ";
            }
            TableModel tableModel=new DefaultTableModel(TB,TH);
            table.setModel(tableModel);
            bo = false;
        }
    }
 
    //按钮2 - 确认退货
    public void retutrns(){
        if(bo){
            DialogSetWindow(d1);
            Label l1 = new Label("是否确定 ?",Label.CENTER);//询问是否确定退货
            Button b1 = new Button("是");//退货
            Button b2 = new Button("否");//取消
            Box vBox = Box.createVerticalBox();
            Box hBox = Box.createHorizontalBox();
            hBox.add(Box.createHorizontalStrut(3));
            hBox.add(b1);
            hBox.add(Box.createHorizontalStrut(3));
            hBox.add(b2);
            hBox.add(Box.createHorizontalStrut(3));
            vBox.add(Box.createVerticalStrut(3));
            vBox.add(l1);
            vBox.add(Box.createVerticalStrut(3));
            vBox.add(hBox);
            vBox.add(Box.createVerticalStrut(3));
            d1.add(vBox);
            d1.setVisible(true); 
            //Caner按钮监听器
            b2.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                        d1.setVisible(false);
                }
            });
            //Yes按钮监听器
            b1.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    DialogSetWindow(d3);
                    Label l2 = new Label("退货成功",Label.CENTER);
                    d3.add(l2);
                    d1.setVisible(false);
                    d3.setVisible(true);
                    //商品库存 + 1
                    SqlUpdata();
                    for(int i=0;i<4;i++){
                        TB[0][i] = " ";
                    }
                    TableModel tableModel=new DefaultTableModel(TB,TH);
                    table.setModel(tableModel);
                    bo = false;
                }
            });   
        }else{
            Label l1 = new Label("请选择交易商品 !",Label.CENTER);//退货管理
            DialogSetWindow(d5);
            d5.add(l1,BorderLayout.CENTER);
            d5.setVisible(true);
        }
    } 
    //数据库库存更新
    public void SqlUpdata(){
        Connection conn=null;
        Statement stat=null;
        try {
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
			stat = conn.createStatement();
            stat.execute("UPDATE sp_goods SET gd_num = gd_num +"+str2+" WHERE gd_bar_code = "+str1);//更新库存
        } catch (Exception ee) {
            System.out.println(ee);
        }
    }
    //主窗口大小位置设置
    public void MainSetWindow(JFrame f){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height)
			frameSize.height = screenSize.height;
		if (frameSize.width > screenSize.width)
			frameSize.width = screenSize.width;
        f.setBounds((screenSize.width - frameSize.width) / 5, (screenSize.height - frameSize.height) / 5,1000,600);
    }
    //对话框大小位置设置
    public void DialogSetWindow(Dialog d){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height)
			frameSize.height = screenSize.height;
		if (frameSize.width > screenSize.width)
			frameSize.width = screenSize.width;
        d.setBounds((screenSize.width - frameSize.width) / 1, (screenSize.height - frameSize.height) / 1,250,100);
    }
}